package org.peipito.prs.engine;

import android.util.Log;


public class GameEngine {

	private static final String TAG = GameEngine.class.getSimpleName();
	
	/**
	 * become false when the game is started.
	 */
	boolean newGame = true; 
	private int playerChoice;
	private int computerChoice;
	private int result;
	
	public int getResult(){
		return result;
	}
	
	public int getComputerChoice(){
		return computerChoice;
	}
	
	
	/**
	 * New Game !!!
	 */
	public void init(){
		GameEngine.gamePhase = GameEngine.COUNTDOWN;
		newGame 		= true;
		playerChoice 	= -10;
		computerChoice 	= -10;
		result 			= -10;
	}

	public void play(int playerChoice){
		this.playerChoice = playerChoice;
		generateComputerChoice();
		generateResult();
		Log.d(TAG,"player choice	" + playerChoice);
		Log.d(TAG,"computer choice	" + computerChoice);
		Log.d(TAG,"result 			" + result);
	}
	
	/**
	 * @return  0 for a win.
	 * @return  1 for a draw.
	 * @return  2 for a loose.
	 */
	private void generateResult(){
		
		if(playerChoice == computerChoice)
			result = 1;
		
		else if(playerChoice == 0){ 	// player choosed rock;
			if(computerChoice == 1)
				result = 2;				// player lost
			else 
				result = 0;				// player won
		}
		
		else if(playerChoice == 1){ 	// player choosed paper;
			if(computerChoice == 2)		
				result =  2;			// player lost
			else 
				result =  0;			// player won
		}	
		
		else if(playerChoice == 2){ 	// player choosed scissor;
			if(computerChoice == 0)
				result = 2;				// player lost
			else 
				result = 0;				// player won
		}	
		
	}
	
	/**
	 * 0 for rock
	 * 1 for paper 
	 * 2 for scissor
	 */
	private void generateComputerChoice(){
		double random = Math.random() * 10;
		
		if (random < 3.3){
			computerChoice = 0;
		} else if(random < 6.6){
			computerChoice = 1;
		} else {
			computerChoice = 2;
		}
	}

	public static int gamePhase = GameEngine.COUNTDOWN;
	
	public static final int COUNTDOWN 	= 0;
	public static final int RESULT		= 1;
	
}
